﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Contoso.Workflow.LocalService;
using System.Security.Permissions;
using System.ComponentModel;
using Contoso.Workflow.Management;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Collections.ObjectModel;
using Contoso.Workflow.Model;

public partial class UserControl_ExpenseButtion : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //控制按钮显示
        EnableButtons();
       
    }

    private void DisableButtons()
    {
        this.btnApproval.Visible = false;
        this.btnSave.Visible = false;
        this.btnReject.Visible = false;
        this.btnBack.Visible = false;
    }

    private void EnableButtons()
    {
        DisableButtons();

        //获得工作流Id
        Guid instanceId = new Guid(Request["InstanceId"]);

        //根据接收的状态动态显示按钮
        WorkflowMgr workflowMgr = new WorkflowMgr();
        ReadOnlyCollection<string> messageList = workflowMgr.GetMessagesAllowed(instanceId);
        if (messageList.Contains("ExpenseApprove")) btnApproval.Visible = true;
        if (messageList.Contains("ExpenseSave")) btnSave.Visible = true;
        if (messageList.Contains("ExpenseReject")) btnReject.Visible = true;
        if (messageList.Contains("ExpenseSendBack")) btnBack.Visible = true;

    }

    private ExpenseData expenseData;
    public ExpenseData UCExpenseData
    {
        get { return expenseData; }
        set { expenseData = value; }
    }

    private string nextUrl;

    public string NextUrl
    {
        get { return nextUrl; }
        set { nextUrl = value; }
    }
	

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ExpenseService expenseService = (ExpenseService)WorkflowMgr.CurrentWorkflowRuntime.GetService(typeof(ExpenseService));
        expenseService.RaiseSaveEvent(expenseData);
        Response.Redirect(NextUrl);
    }
    protected void btnApproval_Click(object sender, EventArgs e)
    {
        ExpenseService expenseService = (ExpenseService)WorkflowMgr.CurrentWorkflowRuntime.GetService(typeof(ExpenseService));
        expenseService.RaiseApproveEvent(expenseData);
        Response.Redirect(NextUrl);
    }
    protected void btnReject_Click(object sender, EventArgs e)
    {
        ExpenseService expenseService = (ExpenseService)WorkflowMgr.CurrentWorkflowRuntime.GetService(typeof(ExpenseService));
        expenseService.RaiseRejectEvent(expenseData);
        Response.Redirect(NextUrl);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        ExpenseService expenseService = (ExpenseService)WorkflowMgr.CurrentWorkflowRuntime.GetService(typeof(ExpenseService));
        expenseService.RaiseSendBackEvent(expenseData);
        Response.Redirect(NextUrl);
    }
}
